﻿using Microsoft.SharePoint;

namespace SKN
{
    /// <summary>
    /// This class will have methods
    /// </summary>
    public static class EventHandler
    {
        /// <summary>
        /// Use this method to add an event handler for Web level
        /// </summary>
        /// <param name="web"></param>
        /// <param name="eventType"></param>
        /// <param name="asmName"></param>
        /// <param name="typeName"></param>
        public static void AddWebEvent(SPWeb web, string eventType, string asmName, string typeName)
        {
            switch (eventType)
            {
                case "SiteDeleted":
                    web.EventReceivers.Add(SPEventReceiverType.SiteDeleted, asmName, typeName);
                    break;
                case "SiteDeleting":
                    web.EventReceivers.Add(SPEventReceiverType.SiteDeleting, asmName, typeName);
                    break;
                case "WebDeleted":
                    web.EventReceivers.Add(SPEventReceiverType.WebDeleted, asmName, typeName);
                    break;
                case "WebDeleting":
                    web.EventReceivers.Add(SPEventReceiverType.WebDeleting, asmName, typeName);
                    break;
                case "WebMoved":
                    web.EventReceivers.Add(SPEventReceiverType.WebMoved, asmName, typeName);
                    break;
                case "WebMoving":
                    web.EventReceivers.Add(SPEventReceiverType.WebMoving, asmName, typeName);
                    break;
            }

            web.Update();
        }

        /// <summary>
        /// Use this method to add an event handler for List/Document Library level
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        /// <param name="eventType"></param>
        /// <param name="asmName"></param>
        /// <param name="typeName"></param>
        public static void AddListEvent(SPWeb web, string listName, string eventType, string asmName, string typeName)
        {
            SPList list = web.Lists[listName];

            switch (eventType)
            {
                case "EmailReceived":
                    list.EventReceivers.Add(SPEventReceiverType.EmailReceived, asmName, typeName);
                    break;
                case "FieldAdded":
                    list.EventReceivers.Add(SPEventReceiverType.FieldAdded, asmName, typeName);
                    break;
                case "FieldAdding":
                    list.EventReceivers.Add(SPEventReceiverType.FieldAdding, asmName, typeName);
                    break;
                case "FieldDeleted":
                    list.EventReceivers.Add(SPEventReceiverType.FieldDeleted, asmName, typeName);
                    break;
                case "FieldDeleting":
                    list.EventReceivers.Add(SPEventReceiverType.FieldDeleting, asmName, typeName);
                    break;
                case "FieldUpdated":
                    list.EventReceivers.Add(SPEventReceiverType.FieldUpdated, asmName, typeName);
                    break;
                case "FieldUpdating":
                    list.EventReceivers.Add(SPEventReceiverType.FieldUpdating, asmName, typeName);
                    break;
                case "ItemAdded":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, typeName);
                    break;
                case "ItemAdding":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAdding, asmName, typeName);
                    break;
                case "ItemAttachmentAdded":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAttachmentAdded, asmName, typeName);
                    break;
                case "ItemAttachmentAdding":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAttachmentAdding, asmName, typeName);
                    break;
                case "ItemAttachmentDeleted":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAttachmentDeleted, asmName, typeName);
                    break;
                case "ItemAttachmentDeleting":
                    list.EventReceivers.Add(SPEventReceiverType.ItemAttachmentDeleting, asmName, typeName);
                    break;
                case "ItemCheckedIn":
                    list.EventReceivers.Add(SPEventReceiverType.ItemCheckedIn, asmName, typeName);
                    break;
                case "ItemCheckedOut":
                    list.EventReceivers.Add(SPEventReceiverType.ItemCheckedOut, asmName, typeName);
                    break;
                case "ItemCheckingIn":
                    list.EventReceivers.Add(SPEventReceiverType.ItemCheckingIn, asmName, typeName);
                    break;
                case "ItemCheckingOut":
                    list.EventReceivers.Add(SPEventReceiverType.ItemCheckingOut, asmName, typeName);
                    break;
                case "ItemDeleted":
                    list.EventReceivers.Add(SPEventReceiverType.ItemDeleted, asmName, typeName);
                    break;
                case "ItemDeleting":
                    list.EventReceivers.Add(SPEventReceiverType.ItemDeleting, asmName, typeName);
                    break;
                case "ItemFileMoved":
                    list.EventReceivers.Add(SPEventReceiverType.ItemFileMoved, asmName, typeName);
                    break;
                case "ItemFileMoving":
                    list.EventReceivers.Add(SPEventReceiverType.ItemFileMoving, asmName, typeName);
                    break;
                case "ItemFileConverted":
                    list.EventReceivers.Add(SPEventReceiverType.ItemFileConverted, asmName, typeName);
                    break;
                case "ItemUncheckedOut":
                    list.EventReceivers.Add(SPEventReceiverType.ItemUncheckedOut, asmName, typeName);
                    break;
                case "ItemUncheckingOut":
                    list.EventReceivers.Add(SPEventReceiverType.ItemUncheckingOut, asmName, typeName);
                    break;
                case "ItemUpdated":
                    list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, typeName);
                    break;
                case "ItemUpdating":
                    list.EventReceivers.Add(SPEventReceiverType.ItemUpdating, asmName, typeName);
                    break;
            }

            list.Update();
            web.Update();
        }

        /// <summary>
        /// Use this method to add an event handler for Content type
        /// </summary>
        /// <param name="web"></param>
        /// <param name="contentTypeName"></param>
        /// <param name="eventType"></param>
        /// <param name="asmName"></param>
        /// <param name="typeName"></param>
        public static void AddContentTypeEvent(SPWeb web, string contentTypeName, string eventType, string asmName,
                                                string typeName)
        {
            SPContentType ct = web.ContentTypes[contentTypeName];


            switch (eventType)
            {
                case "EmailReceived":
                    ct.EventReceivers.Add(SPEventReceiverType.EmailReceived, asmName, typeName);
                    break;
                case "FieldAdded":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldAdded, asmName, typeName);
                    break;
                case "FieldAdding":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldAdding, asmName, typeName);
                    break;
                case "FieldDeleted":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldDeleted, asmName, typeName);
                    break;
                case "FieldDeleting":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldDeleting, asmName, typeName);
                    break;
                case "FieldUpdated":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldUpdated, asmName, typeName);
                    break;
                case "FieldUpdating":
                    ct.EventReceivers.Add(SPEventReceiverType.FieldUpdating, asmName, typeName);
                    break;
                case "ItemAdded":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmName, typeName);
                    break;
                case "ItemAdding":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAdding, asmName, typeName);
                    break;
                case "ItemAttachmentAdded":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAttachmentAdded, asmName, typeName);
                    break;
                case "ItemAttachmentAdding":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAttachmentAdding, asmName, typeName);
                    break;
                case "ItemAttachmentDeleted":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAttachmentDeleted, asmName, typeName);
                    break;
                case "ItemAttachmentDeleting":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemAttachmentDeleting, asmName, typeName);
                    break;
                case "ItemCheckedIn":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemCheckedIn, asmName, typeName);
                    break;
                case "ItemCheckedOut":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemCheckedOut, asmName, typeName);
                    break;
                case "ItemCheckingIn":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemCheckingIn, asmName, typeName);
                    break;
                case "ItemCheckingOut":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemCheckingOut, asmName, typeName);
                    break;
                case "ItemDeleted":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemDeleted, asmName, typeName);
                    break;
                case "ItemDeleting":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemDeleting, asmName, typeName);
                    break;
                case "ItemFileMoved":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemFileMoved, asmName, typeName);
                    break;
                case "ItemFileMoving":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemFileMoving, asmName, typeName);
                    break;
                case "ItemFileConverted":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemFileConverted, asmName, typeName);
                    break;
                case "ItemUncheckedOut":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemUncheckedOut, asmName, typeName);
                    break;
                case "ItemUncheckingOut":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemUncheckingOut, asmName, typeName);
                    break;
                case "ItemUpdated":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemUpdated, asmName, typeName);
                    break;
                case "ItemUpdating":
                    ct.EventReceivers.Add(SPEventReceiverType.ItemUpdating, asmName, typeName);
                    break;
            }
            ct.Update();
            web.Update();
        }

           /// <summary>
        /// Use this method to Delete all the event handlers for a particular Web
        /// </summary>
        /// <param name="web"></param>
        public static void DeleteAllWebEvents(SPWeb web)
        {
            int itemCount = web.EventReceivers.Count;
            for (int k = itemCount - 1; k >= 0; k--)
            {
                web.EventReceivers[k].Delete();
            }
        }
        
        
        /// <summary>
        /// Use this method to Delete all the event handlers for a particular List/Document Library
        /// </summary>
        /// <param name="web"></param>
        /// <param name="listName"></param>
        public static void DeleteAllListEvents(SPWeb web, string listName)
        {
            SPList list = web.Lists[listName];

            int itemCount = list.EventReceivers.Count;
            for (int k = itemCount - 1; k >= 0; k--)
            {
                list.EventReceivers[k].Delete();
            }
        }

        /// <summary>
        /// Use this method to Delete all the event handlers for a particular Content Type
        /// </summary>
        /// <param name="web"></param>
        /// <param name="contentTypeName"></param>
        public static void DeleteAllContentTypeEvents(SPWeb web, string contentTypeName)
        {
            var contentType = web.ContentTypes[contentTypeName];

            int itemCount = contentType.EventReceivers.Count;
            for (int k = itemCount - 1; k >= 0; k--)
            {
                contentType.EventReceivers[k].Delete();
            }
        }
     
    }
}